/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.propertydescriptor;


import java.util.logging.Logger;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.acarter.propertytable.PropertyTable;
import com.acarter.propertytable.PropertyTableModel;
import com.acarter.scenemonitor.information.A_MonitorInformationPanel;

/**
 * @author Carter
 * 
 */
public class PropertyInformation extends A_MonitorInformationPanel {

	/** Logger Name */
	public static final String LOGGER_NAME = "com.acarter.scenemonitor.propertydescriptor.PropertyInformation";

	/** Logger */
	private static final Logger LOGGER = Logger.getLogger(PropertyInformation.LOGGER_NAME);

	/** The property table */
	private PropertyTable table;
	
	/** The scroll pane which contains the property table */
	private JScrollPane tableScrollPane;

	/** Property page handling for each tree object */
	private A_PropertyPageHandler pageHandler;

	/**
	 * A property table is used to display and modify attributes of jME entities.
	 */
	public PropertyInformation() {

		table = new PropertyTable(new PropertyTableModel());
		table.setResizable(false, true);

		// Look for the jME Physics library
		try {
			Class.forName("com.jmex.physics.PhysicsSpace");

			pageHandler = new PhysicsPropertyPageHandler(table);
			LOGGER.info("jME Physics 2 found, using a physics property page descriptor handler.");

		}
		catch (ClassNotFoundException e) {

			pageHandler = new StandardPropertyPageHandler(table);
			LOGGER.info("jME Physics 2 not found, using a standard property page descriptor handler.");
		}
	}

	/**
	 * @see com.acarter.scenemonitor.information.A_MonitorInformationPanel#buildInformationPanel()
	 */
	@Override
	public JPanel buildInformationPanel() {

		tableScrollPane = new JScrollPane(table);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.add(tableScrollPane);
		
		return panel;
	}

	/**
	 * @see com.acarter.scenemonitor.information.A_MonitorInformationPanel#updateInformation(java.lang.Object)
	 */
	@Override
	public void updateInformation(Object object) {

		A_PropertyPage page = pageHandler.getPropertyPage(object);

		if (page != null) {

			int x = tableScrollPane.getHorizontalScrollBar().getValue();
			int y = tableScrollPane.getVerticalScrollBar().getValue();
			
			page.updateInfo(table, object);
			
			tableScrollPane.getHorizontalScrollBar().setValue(x);
			tableScrollPane.getVerticalScrollBar().setValue(y);
		}
	}
	
	/**
	 * Returns the table component of the property information. This is so new cell editors and renderers can 
	 * be added for custom classes, as well as giving access the the tree itself.
	 * 
	 * @return PropertyTable
	 */
	public PropertyTable getPropertyTable() {
		
		return table;
	}
	
	/**
	 * Returns the property page handler, used to manage the relationship between class types and 
	 * the page that is displayed.
	 *  
	 * @return Page Handler
	 */
	public A_PropertyPageHandler getPropertyPageHandler() {
		
		return pageHandler;
	}
}
